<?php

/*
 * Управление авторами
 */

namespace APPS\NEWSPAPER\CONTROLLERS\ADMIN;

use T4\Exception;
use T4\FS\Uploader;

use APPS\NEWSPAPER\MODELS\Author;

class Authors extends \T4\Controller {
    
    public function actionDefault() {
	
	$authors = Author::findAll();
	$this->view->assign('authors', $authors);

        $this->view->assign('author', new Author);
	
	$result['content'] = $this->view->render('admin/authors.html');
        
        return $result;

    }
    
    public function actionSave() {
	
	if ( !empty($_REQUEST['id']) )
	    $author = Author::findByPK(intval($_REQUEST['id']));
	else
	    $author = new Author;
	
        try {
	
            $author->name    = $_POST['name'];
	    $author->surname = $_POST['surname'];

            $uploader = new Uploader();
            $uploader->setUploadPath('newspaper/authors');

            $author->photo = $uploader('photo') ?: $issue->photo;

            $author->save();
            
            $result['content'] = 'Сохранено успешно';
            $result['return'] = '/admin/newspaper/Authors';
            return $result;
        
        } catch ( Exception $e ) {

            $result['errors'][] = $e->getMessage();

        }
	
        $this->view->assign('author', $author);
        $result['content'] = $this->view->render('admin/author.html');
        return $result;

    }
    
    public function actionEdit() {

	$author = Author::findByPK(intval($_REQUEST['id']));
        $this->view->assign('author', $author);
	
	$result['content'] = $this->view->render('admin/author.html');
        return $result;

    }
    
}
